import java.util.ArrayList;
import java.util.Collections;

public class TaskOrganizer {
	
	private ArrayList<PriorityTask> priorityList;
	private ArrayList<CalenderTask> calenderList;
	
	public TaskOrganizer(){
		priorityList = new ArrayList<PriorityTask>();
		calenderList = new ArrayList<CalenderTask>();
	}
	
	public void addNontimedTask(String desc, int prio){
		PriorityTask task = new PriorityTask(desc, prio);
		priorityList.add(task);
		Collections.sort(priorityList);
	}
	
	public void addTimedTask(String desc, int date, int time){
		CalenderTask task = new CalenderTask(desc, time, date);
		calenderList.add(task);
		Collections.sort(calenderList);
	}
	
	public void removeTimedTask(int id){
			calenderList.remove(id);
	}
	
	public void removeNontimedTask(int id){
		priorityList.remove(id);
	}
	
	public void editNontimed(int id, String desc, int prio){
		PriorityTask task = priorityList.get(id);
		task.setDesc(desc);
		task.setPrio(prio);
		Collections.sort(priorityList);
	}

	public void editTimed(int id, String desc, int date, int time){
		CalenderTask task = calenderList.get(id);
		task.setDesc(desc);
		task.setTime(time);
		task.setDate(date);
		Collections.sort(calenderList);
	}

	public String[][] getNontimedTasks(int startPriority, int nrOfTasks){
		return getTasks(false, startPriority, nrOfTasks, priorityList);
	}
	
	public String[][] getTimedTasks(int startDate, int nrOfTasks){
		return getTasks(true, startDate, nrOfTasks, calenderList);
	}
	
	private String[][] getTasks(boolean timed, int startValue, int nrOfTasks, ArrayList list){
		int nrOfArgs;
		if(timed){
			nrOfArgs = 4;
		}
		else{
			nrOfArgs = 3;
		}
		
		int startIndex = -1;
		boolean condition;
		for(int i = 0; i < list.size(); i++){
			if(timed){
				condition = (((CalenderTask) list.get(i)).getValue()>=startValue);
			}
			else{
				condition = (((PriorityTask) list.get(i)).getValue()<=startValue);
			}
			if(condition){
				startIndex = i;
				break;
			}
		}
		if(startIndex == -1){ //if no priority is lower of equal to starting priority, return null
			return null;
		}
		String[][] tasks = new String[nrOfTasks][nrOfArgs];
		for(int i = 0; i < nrOfTasks; i++){
			int index = startIndex + i;
			if((startIndex + i)<list.size()){ //do not go out of range in priorityList
				tasks[i][0] = Integer.toString(index); //convert index(id) of task to String
				tasks[i][1] = ((Task) list.get(index)).getDesc(); //already a String
				if(timed){
					int date = ((CalenderTask) list.get(index)).getDate();
					int t = ((CalenderTask) list.get(index)).getTime();
					tasks[i][2] = String.format(String.format("%%0%dd", 6), date); 
					tasks[i][3] = String.format(String.format("%%0%dd", 4), t);
				}
				else{
					tasks[i][2] = Integer.toString(((PriorityTask) list.get(index)).getPrio()); //convert prio/date of task to String
				}
			}
		}
		return tasks;
	}
	
	private void load(){

	}
	
	private void store(){
		
	}
	
}

